--[[ 圆桌骑士脚本,显示乱步值和HP

  By setycyas @2025-07-14
]]

--防止重复运行
if (_G.game) then
  print('Knights.lua is running!')
  return
end
--模块调用
local myMame = require('MyMame')
-- 本模块,定义时设定基本参数
local GAME = {
  -- 脚本名,提示与分辨用
  NAME = "knights_autoboot",
  READ_TYPE = 0, --0表示读取1字节,否则读取2字节
  -- 分辨率
  X_RESOLUTION = 384,
  -- 乱步,HP地址
  RANDOM_ADDR = 0xFF89D6,
  HP_ADDR = 0xFFABA5,
  -- 显示数据的位置坐标
  RANDOM_X = 0.3,
  RANDOM_Y = 0.15,
  HP_X = 0.4,
  HP_Y = 0.07,
  -- 字体颜色
  FONT_COLOR = 0xFFffff00,
  -- 线颜色
  LINE_COLOR = 0xFF000000,
  
  -- 更新模块参数函数
  refreshModule = function(self)
    myMame.gameName = self.NAME
    myMame.screenIndex = ':screen'
    myMame.mem = manager.machine.devices[':maincpu'].spaces['program']
  end
}
-- 更新模块参数
GAME:refreshModule()

-- 显示乱步
function GAME.showRandom()
  local rand = string.format("%X", myMame.mem:read_u8(GAME.RANDOM_ADDR))
  myMame.ui_container:draw_text(
    GAME.RANDOM_X, GAME.RANDOM_Y, 
    rand, GAME.FONT_COLOR
  )
end
-- 显示血量
function GAME.showHP()
  local hp = string.format("%d", myMame.mem:read_u8(GAME.HP_ADDR))
  myMame.ui_container:draw_text(
    GAME.HP_X, GAME.HP_Y, 
    hp, GAME.FONT_COLOR
  )
end

-- 主函数,绑定后每一帧执行一次该函数
function GAME.Main()
  
  GAME.showRandom()
  GAME.showHP()

end

-- [[运行]]

-- 绑定每一帧执行的函数
--myMame.debugRunPerFrame(GAME.Main, 'dino')
myMame.runPerFrame(GAME.Main, 'knights')
-- 导出到全局变量
_G.game = GAME

return GAME
